angular.module('com.dcos.app').directive('layoutMenu', function LayoutMenu() {
  return {
    restrict: 'EA',
    replace: true,
    scope: {},
    templateUrl: 'directive/layout-menu/menu.html',
    controller: 'LayoutMenuController as vm'
  };
}).directive('secondMenu', function () {
	return {
    	restrict: 'A',
		scope: {
            open: '='
        },
        link: function (scope, element, attrs) {
        	element = $(element);

            scope.calHeight = function (ulPanel) {
                if (parseInt(ulPanel.height()) === 0) {
                    if (ulPanel.length > 0) {
                        var liItems = ulPanel.children('li')
                        var height = 0;
                        angular.forEach(liItems, function (item) {
                            height += parseFloat($(item).height());
                        });
                        ulPanel[0].style.height = height + 'px';
                    }
                } else {
                    ulPanel.height('0px');
                }
            }

            scope.open && scope.calHeight(element.find('ul'));

            element.parent('ul').children('li').off('click');
        	element.parent('ul').children('li').on('click', function () {
    	    	if (sessionStorage.getItem('navbarStatus') !== 'narrow') {
    	    		$(this).siblings().find('ul').height('0px');
                    $(this).siblings().removeClass('open');

                    $(this).hasClass('open') ? $(this).removeClass('open') : $(this).addClass('open');

    			    // 若存在二级菜单，计算高度
                    scope.calHeight($(this).find('ul'));
                }
        	});

        	element.find('li').on('click', function (e) {
                e.stopPropagation();
                sessionStorage.getItem('navbarStatus') === 'narrow' && element.find('ul').hide();
        	});

        	element.hover(function () {
    	    	sessionStorage.getItem('navbarStatus') === 'narrow' && $(this).find('ul').show();
        	}, function () {
    	    	sessionStorage.getItem('navbarStatus') === 'narrow' && $(this).find('ul').hide();
        	});
        }
	}
});
